﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using System.Collections;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;

namespace Homework4
{
    public partial class Form1 : Form
    {
        private ILayer m_Layer;
        private Boolean isStart = false;
        private Boolean isFirstDown;
        public IFeatureClass pFClass;//定义为公共的，让form2可以使用
        short transparency = 0;//透明度 
        public Form1()
        {
            InitializeComponent();
        }

        private void axLicenseControl1_Enter(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap pBasicMap = null;
                ILayer pLayer = null;
                object other = null;
                object index = null;
                axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);          //实现赋值
                m_Layer = pLayer;
                IFieldInfoSet pFields = pLayer as FieldInfoSetClass;
                if (Item == esriTOCControlItem.esriTOCControlItemLayer)           //点击的是图层的话，就显示右键菜单
                {
                    contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                    //显示右键菜单，并定义其相对控件的位置，正好在鼠标出显示
                }
                //else if (Item == esriTOCControlItem.esriTOCControlItemLegendClass)
                //{
                //    contextMenuStrip2.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                //}
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            //axTOCControl1.Width = this.Width / 5 * 2;
            ////axTOCControl1.Height = this.Height / 3 * 2;
            //axToolbarControl1.Width = this.Width / 5 * 3;
            ////axToolbarControl1.Height = this.Height;
            //groupBox1.Width = this.Width / 5 * 2;
            ////groupBox1.Height = this.Height / 3;
            //tabControl1.Width = this.Width / 5 * 2;
            ////tabControl1.Height = this.Height;
        }

        private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                 if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引，并直接调用DeleteLayer方法定点删除
                 axMapControl1.DeleteLayer(i);
            }
            axMapControl1.ActiveView.Refresh();
        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                
                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引
                    toIndex = i;
            }
            if (toIndex > 1)
            {
                toIndex--;//上移减一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        private void 下移图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {

                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引
                    toIndex = i;
            }
            if (toIndex < axMapControl1.LayerCount)
            {
                toIndex++;//下移加一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            axToolbarControl1.AddMenuItem("esriControls.ControlsMapViewMenu", -1, false, -1);

        }

        
/*
 *
 * 实验四代码部分
 * 
 */
        /*
         * 地图取点
         */
        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            radioButton3.Enabled = true;
            isStart = true;
        }
        //当点击了start按钮后，开始记录点击的点坐标
        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            //必须是点击了start按钮才能进行画点、线、面
            if (isStart)
            {
                IMap pMap;
                IActiveView pActiveView;
                pMap = axMapControl1.Map;
                pActiveView = pMap as IActiveView;
                IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
                //画点
                if (radioButton1.Checked)
                {

                    IPoint pPoint = axMapControl1.ToMapPoint(e.x, e.y);
                    IMarkerElement pMarkerElement = new MarkerElementClass();
                    IElement pElement = pMarkerElement as IElement;
                    pElement.Geometry = pPoint;
                    pGraphicsContainer.AddElement((IElement)pMarkerElement, 0);
                    pActiveView.Refresh();
                }
                //画多义线
                //双击后停止画多义线
                if (radioButton2.Checked && !isFirstDown)
                {
                    IGeometry polyline;
                    polyline = axMapControl1.TrackLine();
                    ILineElement pLineElement;
                    pLineElement = new LineElementClass();
                    IElement pElement;
                    pElement = pLineElement as IElement;
                    pElement.Geometry = polyline;

                    pGraphicsContainer.AddElement((IElement)pLineElement, 0);
                    pActiveView.Refresh();
                }

                if (radioButton3.Checked && !isFirstDown)
                {
                    IGeometry Polygon = axMapControl1.TrackPolygon();
                    IPolygonElement PolygonElement = new PolygonElementClass();
                    IElement pElement = PolygonElement as IElement;
                    pElement.Geometry = Polygon;
                    pGraphicsContainer = pMap as IGraphicsContainer;
                    pGraphicsContainer.AddElement((IElement)PolygonElement, 0);
                    pActiveView.Refresh();

                }
                isFirstDown = false;
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            isFirstDown = true;
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            isFirstDown = true;
        }
        //点击stop按钮后
        private void button2_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            radioButton3.Enabled = false;
            isStart = false;
        }
        /*
         * 缩放地图
         */
        private void button3_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = true;
            radioButton5.Enabled = true;
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                ICommand pCommand = new ControlsMapZoomInToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                ICommand pCommand = new ControlsMapZoomOutToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }

        //结束
        private void button4_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = false;
            radioButton5.Enabled = false;
        }

        public void 修改图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = m_Layer as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            
            Form2 form2 = new Form2(pFClass);
            form2.Show();
        }

        private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
        {
            IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);

            ISpatialFilter spatialFilter = new SpatialFilter();
            spatialFilter.Geometry = point;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, false);

            IFeature pFeature;
            while((pFeature=featureCursor.NextFeature())!=null)
            {
                axMapControl1.FlashShape(pFeature.Shape);
                String text = "";
                for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                {
                    text += pFeature.Fields.Field[i].Name + ":" + pFeature.get_Value(i) + "\n";
                }
                     
                MessageBox.Show(text);
            }
        }

        private void 查询人口数量大于2500的区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            IFields pFields = pFClass.Fields;
            IFeatureCursor pFeaCursor = pFClass.Search(null, true);
            IFeature pFeature = pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                if (pFeature.get_Value(pFeature.Fields.FindField("pop")) > 2500)
                {
                    axMapControl1.FlashShape(pFeature.Shape);
                }
                pFeature = pFeaCursor.NextFeature();
            }
        }

        private void 查询面积大于25000000平方米的区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            IFields pFields = pFClass.Fields;
            IFeatureCursor pFeaCursor = pFClass.Search(null, true);
            IFeature pFeature = pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                if (pFeature.get_Value(pFeature.Fields.FindField("area")) > 25000000)
                {
                    axMapControl1.FlashShape(pFeature.Shape);
                }
                pFeature = pFeaCursor.NextFeature();
            }
        }

        private void 增加透明度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transparency += 10;
            ILayer pLayer = this.axMapControl1.get_Layer(0);　
            ILayerEffects pLayerEffects = pLayer as ILayerEffects;
            short sp = transparency;
            pLayerEffects.Transparency = transparency;
            axMapControl1.Refresh();
                
        }

        private void 减小透明度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transparency -= 10;
            ILayer pLayer = this.axMapControl1.get_Layer(0);　
            ILayerEffects pLayerEffects = pLayer as ILayerEffects;
            short sp = pLayerEffects.Transparency;
            pLayerEffects.Transparency = transparency;
            axMapControl1.Refresh();
        }


        ////点击图层要素高亮并弹出属性信息
        //private void 要素高亮(IMapControlEvents2_OnMouseDownEvent e)
        //{
        //    ITopologicalOperator pTopo;
        //    IGeometry pGeometry;
        //    IFeature pFeature;
        //    IFeatureLayer pFeatureLayer;
        //    IFeatureCursor pCursor;
        //    DataTable dataTable;
        //    for (int i = 0; i < axMapControl1.Map.LayerCount; i++)
        //    {
        //        //获取鼠标点击位置
        //        IPoint pPoint = new PointClass();
        //        pPoint.PutCoords(e.mapX, e.mapY);
        //        pTopo = pPoint as ITopologicalOperator;
        //        double m_Radius = 1;
        //        //获取点击的Geo
        //        pGeometry = pTopo.Buffer(m_Radius);
        //        if (pGeometry == null)
        //            continue;

        //        axMapControl1.Map.SelectByShape(pGeometry, null, true);//第三个参数为是否只选中一个
        //        axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); //选中要素高亮显示
        //        pFeatureLayer = axMapControl1.Map.get_Layer(i) as IFeatureLayer;
        //        pCursor = pFeatureLayer.Search(null, false);
        //        pFeature = pCursor.NextFeature();
        //    }
        //}
    }
}
